import { ShellUtil } from "./ShellUtil";
import { Main } from "../Main";

/**
 * 资源加密工具
 * @author 赵占涛
 */
export class EncryptResUtil {
    /** path:传入res和src所在的Resources目录 */
    static exec(path: string, cb: () => void) {
        this.execForLua(path + "src/", () => {
            this.execForLua(path + "res/ccs/views/", () => {
                this.execForRes(path + "res/", () => {
                    cb();
                })
            })
        })
    }
    private static execForRes(path: string, cb: () => void) {
        ShellUtil.exec(`
        rm -rf ${Main.projPath}easyscript/temp/
        mv -f ${path} ${Main.projPath}easyscript/temp/ || exit
        `, () => {
            ShellUtil.spawn("php", [
                `${Main.projPath}easyscript/lib/cocoslib/encrypt_res.php`,
                "-i",
                `${Main.projPath}easyscript/temp/`,
                "-o",
                path,
                "-es",
                "h98y8o9yeeqiie3t",
                "-ek",
                "zdjhgrs3sc72pll0"
            ], cb);
        });
    }

    private static execForLua(path: string, cb: () => void) {
        ShellUtil.exec(`
        rm -rf ${Main.projPath}easyscript/temp/
        mv -f ${path} ${Main.projPath}easyscript/temp/ || exit
        cocos luacompile -s ${Main.projPath}easyscript/temp/ -d ${path} -e -k keyj7ge3ghk -b signbvsjy78e4jvywl --encrypt --disable-compile || exit 1
        `, cb);
    }
}